package com.admin.testproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import com.admin.testproject.OnItemClickListener;

import java.util.List;

/**
 * 作者： 吴昶 .
 * 时间: 2019/2/11 17:27
 * 功能简介：
 */
public abstract class RecycleBaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    private List<T> data;
    public Context context;
    public OnItemClickListener onItemClick;
    public abstract int getItemLayout(int position);

    public void upData(List<T> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public RecycleBaseAdapter(Context context){
        this.context=context;
    }

    @Override
    public int getItemViewType(int position) {
        //可以通过修改此处的返回值，来改变 需要加载的 页面类型，
        // 然后在getItemLayout中判断相同的值的时候返回所需的 布局ID
        return position;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return BaseViewHolder.getHolder(viewGroup,getItemLayout(i));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, int i) {
        if(viewHolder!=null && data.get(i)!=null) {
            initData(viewHolder, data.get(i), i);
        }
    }

    public abstract void initData(BaseViewHolder viewHolder, T item, int position);

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

}
